#include "StdAfx.h"
#include "UIWidget.h"

CEGUI::Window* Create(const CEGUI::String& type,const CEGUI::String& name)
{
	return CEGUI::WindowManager::getSingleton().createWindow(type,name);
}

CEGUI::Window* GetRootWidget()
{
	return CEGUI::System::getSingleton().getGUISheet();
}

void Destory(CEGUI::Window* window)
{
	return CEGUI::WindowManager::getSingleton().destroyWindow(window);
}

void DestoryByName(const CEGUI::String windowName)
{
	return CEGUI::WindowManager::getSingleton().destroyWindow(windowName);
}

bool IsWidgetExists(const CEGUI::String& windowName)
{
	return CEGUI::WindowManager::getSingleton().isWindowPresent(windowName);	
}

CEGUI::WindowManager::WindowIterator GetWindowIterator()
{
	return CEGUI::WindowManager::getSingleton().getIterator();
}

void WindowIterator_Next(CEGUI::WindowManager::WindowIterator* self)
{
	self++;
}

void WindowIterator_Previous(CEGUI::WindowManager::WindowIterator* self)
{
	self--;
}

void ScriptBackend::Module_UIWidget( lua_State* l )
{
	using namespace luabind;
	using namespace CEGUI;

	module(l)
		[
			class_<CEGUI::WindowManager::WindowIterator>("UIWidgetIterator")
			.property("CurrentKey",&WindowManager::WindowIterator::getCurrentKey)
			.property("CurrentValue",&WindowManager::WindowIterator::getCurrentValue)
			.property("IsAtEnd",&WindowManager::WindowIterator::isAtEnd)
			.property("IsAtStart",&WindowManager::WindowIterator::isAtStart)
			.def("ToEnd",&WindowManager::WindowIterator::toEnd)
			.def("ToStart",&WindowManager::WindowIterator::toStart)
			.def("Next",&WindowIterator_Next)
			.def("Previous",&WindowIterator_Previous)
			.def(const_self == other<const WindowManager::WindowIterator&>())
			,
			class_<Window,bases<PropertySet,EventSet>>("UIWidget")			
			.property("AlwaysOnTop",&Window::isAlwaysOnTop,&Window::setAlwaysOnTop)
			.property("Visible",&Window::isVisible,&Window::setVisible)			
			.property("Position",&Window::getPosition,&Window::setPosition)
			.property("Rotation",&Window::getRotation,&Window::setRotation)
			.property("Size",&Window::getSize,&Window::setSize)
			.property("MaxSize",&Window::getMaxSize,&Window::setMaxSize)
			.property("MinSize",&Window::getMinSize,&Window::setMinSize)
			.property("X",&Window::getXPosition,&Window::setXPosition)
			.property("Y",&Window::getYPosition,&Window::setYPosition)
			.property("Width",&Window::getWidth,&Window::setWidth)
			.property("Height",&Window::getHeight,&Window::setHeight)
			.property("PixelSize",&Window::getPixelSize)	
			.property("Parent",&Window::getParent)			
			.property("ChildCount",&Window::getChildCount)
			.property("Name",&Window::getName)
			.property("Type",&Window::getType)
			.property("Disabled",&Window::isDisabled)
			.property("Active",&Window::isActive)			
			.property("Alpha",&Window::getAlpha,&Window::setAlpha)
			.property("ID",&Window::getID,&Window::setID)
			.property("ClippedByParent",&Window::isClippedByParent,&Window::setClippedByParent)
			.property("Text",&Window::getText,&Window::setText)			
			.property("Font",&Window::getFont,(void(Window::*)(Font*))&Window::setFont)
			.property("MouseCursor",&Window::getMouseCursor,(void(Window::*)(const Image*))&Window::setMouseCursor)
			.property("TooltipText",&Window::getTooltipText,&Window::setTooltipText)
			.property("Tooltip",&Window::getTooltip,&Window::setTooltip)
			.property("HorizontalAlignment",&Window::getHorizontalAlignment,&Window::setHorizontalAlignment)
			.property("VerticalAlignment",&Window::getVerticalAlignment,&Window::setVerticalAlignment)
			.property("UserData",&Window::getUserData,&Window::setUserData)
			.property("UserString",&Window::getUserString,&Window::setUserString)
			.def("Show",&Window::show)
			.def("Hide",&Window::hide)
			.def("Activate",&Window::activate)
			.def("Deactivate",&Window::deactivate)
			.def("Enable",&Window::enable)
			.def("Disable",&Window::disable)
			.def("InsertText",&Window::insertText)
			.def("AppendText",&Window::appendText)
			.def("IsHit",&Window::isHit)
			.def("GetChild",(Window*(Window::*)(const String&)const)&Window::getChild)
			.def("GetChildAt",&Window::getChildAtIdx)
			.def("GetChildAtPostion",&Window::getChildAtPosition)			
			.def("AddChild",(void(Window::*)(Window*))&Window::addChildWindow)
			.def("RemoveChild",(void(Window::*)(Window*))&Window::removeChildWindow)
			.def("Destory",&Destory)
			.enum_("HorizontalAlignment")
			[
				value("HA_LEFT",CEGUI::HorizontalAlignment::HA_LEFT),
				value("HA_CENTRE",CEGUI::HorizontalAlignment::HA_CENTRE),
				value("HA_RIGHT",CEGUI::HorizontalAlignment::HA_RIGHT)
			]
			.enum_("VerticalAlignment")
				[
					value("VA_TOP",CEGUI::VerticalAlignment::VA_TOP),
					value("VA_CENTRE",CEGUI::VerticalAlignment::VA_CENTRE),
					value("VA_BOTTOM",CEGUI::VerticalAlignment::VA_BOTTOM)
				]
			.scope
				[
					def("Create",&Create),
					def("IsWidgetExists",&IsWidgetExists),
					def("GetRootWidget",&GetRootWidget),
					def("GetWidgetIterator",&GetWindowIterator)
				]
			// 			,
			// 			class_<WindowManager>("WindowManager")
			// 			.def("createWindow",&WindowManager::createWindow)
			// 			.def("destroyWindow",(void(WindowManager::*)(Window*))&WindowManager::destroyWindow)
			// 			.def("destroyWindow",(void(WindowManager::*)(const String&))&WindowManager::destroyWindow)
			// 			//.def("getWindow",&WindowManager::getWindow)
			// 			.def("isWindowPresent",&WindowManager::isWindowPresent)
			// 			.scope
			// 			[
			// 				def("getSingleton",&WindowManager::getSingleton)
			// 			]
		];
}